﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xeml.Document;
using System.Data.SqlClient;

namespace XemlTest
{
    class General
    {
        #region members
        static object _sync = new object();
        #endregion members

        /// <summary>
        /// IDs from the enumeration
        /// </summary>
        public static IEnumerable<Guid> GetXemlIds()
        {
            yield return Guid.Parse("57545514-4007-4234-9ba0-f37fb2794081");    //7311if - Ea27a
            yield return Guid.Parse("5f135f69-aff6-4d0a-8f7a-2f54be22d3e9");    //7311if - Ea27b
            yield return Guid.Parse("747b1c79-3ef5-486f-b5ba-4faf0b0f55e5");    //7311if - Ea27c

            yield return Guid.Parse("BA487064-8B46-4408-812C-B4E5083AD7C2");    //6308if - A
            yield return Guid.Parse("46ace0ac-a24b-4c19-a67f-7f8af3402641");    //6308if - B
            yield return Guid.Parse("cdb1fa79-b1a7-4e18-9caf-54eb2f3bb6a1");    //6308if - C

            yield return Guid.Parse("10b38aaf-b977-4950-85b8-f4775f66658d");
            yield return Guid.Parse("B38DF13D-55B6-47D2-9E6A-5EF878ECAEAB");
            yield return Guid.Parse("6D1D1929-3EDF-4CB2-A134-78D2BC1AA0C2");

            //this is a proprietary one
            yield return Guid.Parse("cf0f592d-7883-4134-916f-3af8f0ad1c98");        //isabell orf
            yield return Guid.Parse("061dbdeb-f3dd-4afa-acfd-54e83479c3dd"); // DFG single leaf analysis

            yield break;
        }

        /// <summary>
        /// IDs fro the database
        /// </summary>
        public static IEnumerable<Guid> GetXemlIds(SqlConnection con)
        {
            using (SqlCommand cmd = new SqlCommand())
            using (cmd.Connection = con)
            {
                cmd.CommandText = @"SELECT XemlId FROM xeml.Document";
                cmd.CommandType = System.Data.CommandType.Text;

                try
                {
                    cmd.Connection.Open();
                    using (cmd.Transaction = cmd.Connection.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
                    using (System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection | System.Data.CommandBehavior.SequentialAccess | System.Data.CommandBehavior.SingleResult))
                    {
                        while (reader.Read())
                        {
                            yield return reader.GetGuid(0);
                        }
                    }
                }
                finally
                {
                    cmd.Connection.Close();
                }
            }
        }

        /// <summary>
        /// returns the XEML xml from the database
        /// </summary>
        public static string GetXeml(Guid XemlId, string connectionString)
        {
            using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
            using (cmd.Connection = new System.Data.SqlClient.SqlConnection(connectionString))
            {
                cmd.CommandText = @"SELECT Xeml FROM xeml.Document WHERE XemlId = @XemlId";
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.Parameters.AddWithValue("@XemlId", XemlId);

                try
                {
                    cmd.Connection.Open();
                    using (cmd.Transaction = cmd.Connection.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
                    {
                        return cmd.ExecuteScalar() as string;
                        //return (cmd.ExecuteScalar() as string).Replace("<xeml:EnvironmentalOntology NS=\"PO\"", "<xeml:EnvironmentalOntology NS=\"EO\"").Replace("NS=\"PO\"","NS=\"EO\"");
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    cmd.Connection.Close();
                }
            }
        }

        public static XemlDocument GetDocument(Guid xemlId)
        {
            lock (_sync)
            {
                return new XemlDocument(GetXeml(xemlId, Properties.Settings.Default.GmdConnectionString));
            }
        }
    }
}
